/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Edder
 */
public class Conexion {
    private String driver;
    private String connectString;
    private String user;
    private String pass;
    private Connection cnx;
    
    public static final int MYSQL = 1;
    public static final int POSTGRESQL = 2;
    public static final int SQLITE = 3;
    
    public Conexion(String host, int puerto, String baseDatos,
            String user, String pass, int gestor){
        this.connectString = "";
        this.driver = "";
        
        switch(gestor){
            case MYSQL:
                this.driver = "com.mysql.jdbc.Driver";
                this.connectString = "jdbc:mysql://" + host + ":" + puerto;
                this.connectString += "/" + baseDatos;
                break;                
            case POSTGRESQL:
                this.driver = "org.postgresql.Driver";
                this.connectString = "jdbc:postgresql://" + host + ":" + puerto;
                this.connectString += "/" + baseDatos;
                break;
            case SQLITE:
                this.driver = "org.sqlite.JDBC";
                this.connectString = "jdbc:sqlite:" + baseDatos;
                break;
        }
        
        this.user = user;
        this.pass = pass;
        this.cnx = null;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
    
    public Connection getConexion(){
        return this.cnx;
    }
    
    public void abrirConexion() throws ClassNotFoundException, SQLException{
        Class.forName(this.driver);
        this.cnx = DriverManager.getConnection(this.connectString, this.user,
                this.pass);
    }
    
    public void cerrarConexion() throws SQLException{
        this.cnx.close();        
    }
    
    public ResultSet consultar(String sql) throws SQLException{
        Statement stmt = this.cnx.createStatement();
        return stmt.executeQuery(sql);
    }
    
    public ResultSet conectarConsultar(String sql) throws ClassNotFoundException, SQLException{
        this.abrirConexion();
        return this.consultar(sql);
    }
    
    public void ejecutar(String sql) throws SQLException{
        Statement stmt = this.cnx.createStatement();
        stmt.execute(sql);
    }
    
    public void conectarEjecutar(String sql) throws ClassNotFoundException, SQLException{
        this.abrirConexion();
        this.ejecutar(sql);
    }
    
    public void abrirTransaccion() throws SQLException{
        this.cnx.setAutoCommit(false);
    }
    
    public void cerrarTransaccion() throws SQLException{
        this.cnx.commit();
        this.cnx.setAutoCommit(true);
    }
        
    public static Conexion getConexionDefault(){
        return new Conexion("192.168.1.45", 3306, "bodega", "root", "", MYSQL);
    }
}